?><?

class cls_db_entity{

	function __construct(){
		
	}

	function entity(){return "undefined";}

	function data_fields(){return array();}

	function get_var($field_name){
		$entity		= $this->entity();
		if(strpos($field_name,$entity."_")===0){
			$loc_name=substr($field_name,strlen($entity."_"));
		}else{
			$loc_name=$field_name;
		}
		if(isset($this->$loc_name)){
			return $this->$loc_name;
		}else{
			return null;
		}
	}

	function set_var($entity,$field_name,$value){
		if(strpos($field_name,$entity."_")===0){
			$loc_name=substr($field_name,strlen($entity."_"));
		}else{
			$loc_name=$field_name;
		}
		$this->$loc_name=$value;
	}

	function build_insert_field_list($entity,$data){	
	
	}

	function prepare_insert_data($entity,$data,$data_fields){
		$user=get_user();
		$user_id=$user->id;
		$visitor_id=get_visitor();
		foreach($data_fields as $name){			
			if($name!=$entity."_id"){
				switch($name){
					case $entity."_created":{if(!isset($data[$entity."_created"])){$data[$entity."_created"]="Now()";}break;}
					case $entity."_updated":{if(!isset($data[$entity."_updated"])){$data[$entity."_updated"]="Now()";}break;}
					case "creator_id"			:{if(!isset($data["creator_id"])){$data["creator_id"]=$user_id;}break;}
					case $entity."_uid"		:{if(!isset($data[$entity."_uid"])){$data[$entity."_uid"]=uniqid("");}break;}
					case "user_id"				 :{if(!isset($data["user_id"])){$data["user_id"]=$user_id;}break;}
					case "visitor_id"			:{if(!isset($data["visitor_id"])){$data["visitor_id"]=$visitor_id;}break;}				
				}
			}
		}
		return $data;
	}

	function get_entity_data_fields($entity){return eval("return cls_".$entity."_item::data_fields();");}
	function get_insert_names($data) {}
	function get_insert_values($data){}
	function getMethodOutput($method){ob_start();$this->$method();$content=ob_get_contents();ob_end_clean();return $content;}
}